{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From <ipython-input-1-0437e6879596>:5: read_data_sets (from tensorflow.examples.tutorials.mnist.input_data) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use alternatives such as: tensorflow_datasets.load('mnist')\n",
      "WARNING:tensorflow:From D:\\ProgramData\\Anaconda3\\lib\\site-packages\\tensorflow\\examples\\tutorials\\mnist\\input_data.py:296: _maybe_download (from tensorflow.examples.tutorials.mnist.input_data) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please write your own downloading logic.\n",
      "WARNING:tensorflow:From D:\\ProgramData\\Anaconda3\\lib\\site-packages\\tensorflow\\examples\\tutorials\\mnist\\input_data.py:299: _extract_images (from tensorflow.examples.tutorials.mnist.input_data) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use tf.data to implement this functionality.\n",
      "Extracting MNIST_data\\train-images-idx3-ubyte.gz\n",
      "WARNING:tensorflow:From D:\\ProgramData\\Anaconda3\\lib\\site-packages\\tensorflow\\examples\\tutorials\\mnist\\input_data.py:304: _extract_labels (from tensorflow.examples.tutorials.mnist.input_data) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use tf.data to implement this functionality.\n",
      "Extracting MNIST_data\\train-labels-idx1-ubyte.gz\n",
      "WARNING:tensorflow:From D:\\ProgramData\\Anaconda3\\lib\\site-packages\\tensorflow\\examples\\tutorials\\mnist\\input_data.py:112: _dense_to_one_hot (from tensorflow.examples.tutorials.mnist.input_data) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use tf.one_hot on tensors.\n",
      "Extracting MNIST_data\\t10k-images-idx3-ubyte.gz\n",
      "Extracting MNIST_data\\t10k-labels-idx1-ubyte.gz\n",
      "WARNING:tensorflow:From D:\\ProgramData\\Anaconda3\\lib\\site-packages\\tensorflow\\examples\\tutorials\\mnist\\input_data.py:328: _DataSet.__init__ (from tensorflow.examples.tutorials.mnist.input_data) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use alternatives such as official/mnist/_DataSet.py from tensorflow/models.\n",
      "Iter0,Testing Accuray0.9289\n",
      "Iter1,Testing Accuray0.9296\n",
      "Iter2,Testing Accuray0.9295\n",
      "Iter3,Testing Accuray0.9303\n",
      "Iter4,Testing Accuray0.9299\n",
      "Iter5,Testing Accuray0.9301\n",
      "Iter6,Testing Accuray0.9294\n",
      "Iter7,Testing Accuray0.9299\n",
      "Iter8,Testing Accuray0.9299\n",
      "Iter9,Testing Accuray0.9298\n",
      "Iter10,Testing Accuray0.9296\n",
      "Iter11,Testing Accuray0.93\n",
      "Iter12,Testing Accuray0.9294\n",
      "Iter13,Testing Accuray0.9294\n",
      "Iter14,Testing Accuray0.9294\n",
      "Iter15,Testing Accuray0.9289\n",
      "Iter16,Testing Accuray0.9295\n",
      "Iter17,Testing Accuray0.9291\n",
      "Iter18,Testing Accuray0.9288\n",
      "Iter19,Testing Accuray0.9285\n",
      "Iter20,Testing Accuray0.9284\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "from tensorflow.examples.tutorials.mnist import input_data\n",
    "tf.compat.v1.disable_eager_execution()\n",
    "#下载数据集\n",
    "mnist = input_data.read_data_sets('MNIST_data',one_hot=True)\n",
    "\n",
    "\n",
    "\n",
    "#每个批次的大小\n",
    "batch_size = 100\n",
    "#计算一共有多个批次  //batch_size\n",
    "n_batch = mnist.train.num_examples\n",
    "#命名空间\n",
    "with tf.compat.v1.name_scope('input'):\n",
    "    x = tf.compat.v1.placeholder(tf.float32,[None,784],name='x-input')\n",
    "    #0-9 10个数字\n",
    "    y = tf.compat.v1.placeholder(tf.float32,[None,10],name='y-input')\n",
    "\n",
    "with tf.name_scope('layer'):\n",
    "    #创建一个简单的神经网络\n",
    "    with tf.name_scope('wights'):\n",
    "        W = tf.Variable(tf.zeros([784,10]))\n",
    "    with tf.name_scope('biases'):\n",
    "    \n",
    "        b = tf.Variable(tf.zeros([10]))\n",
    "    with tf.name_scope('wx_plus_b'):\n",
    "        wx_plus_b = tf.matmul(x, W)+b\n",
    "        \n",
    "    with tf.name_scope('softmax'):\n",
    "        prediction = tf.nn.softmax(wx_plus_b)\n",
    "#二次代阶函数\n",
    "# loss = tf.reduce_mean(tf.square(y-prediction))\n",
    "#交叉熵代价函数\n",
    "with tf.name_scope('loss'):\n",
    "    loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=prediction))\n",
    "with tf.name_scope('train'):\n",
    "    #使用梯度下降法\n",
    "    train_step = tf.compat.v1.train.GradientDescentOptimizer(0.2).minimize(loss)\n",
    "#初始化变量\n",
    "init = tf.compat.v1.global_variables_initializer()\n",
    "#\n",
    "with tf.name_scope('accuracy'):\n",
    "    with tf.name_scope('correct_prediction'):\n",
    "        correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(prediction,1))\n",
    "    #求准确率\n",
    "    with tf.name_scope('accuracy'):\n",
    "        accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))\n",
    "\n",
    "with tf.compat.v1.Session() as sess:\n",
    "    sess.run(init)\n",
    "    \n",
    "    writer = tf.compat.v1.summary.FileWriter('log/',sess.graph)\n",
    "    for epoch in range(21):\n",
    "        for batch in range (n_batch):\n",
    "            batch_xs,batch_ys = mnist.train.next_batch(batch_size)\n",
    "            sess.run(train_step,feed_dict={x:batch_xs,y:batch_ys})\n",
    "        acc = sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels})\n",
    "        print('Iter' +str(epoch)+',Testing Accuray' +str(acc))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "tensorboard --logdir='log/'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "73e03da126b73bfff3642ec5261d56fa25c444ea595de51041687efaa60dda41"
  },
  "kernelspec": {
   "display_name": "Python 3.8.8 64-bit ('base': conda)",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
